Skip to contentMethod: removeAnnotationAssertions(OWLNamedIndividual, Assertion, Set)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.owlapi;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.AbstractAxiomDescriptor;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.NamedResource;
23: import cz.cvut.kbss.ontodriver.model.Value;
24: import cz.cvut.kbss.ontodriver.owlapi.change.MutableRemoveAxiom;
25: import cz.cvut.kbss.ontodriver.owlapi.change.TransactionalChange;
26: import cz.cvut.kbss.ontodriver.owlapi.change.SubjectAnnotationPropertyRemove;
27: import cz.cvut.kbss.ontodriver.owlapi.change.SubjectClassAssertionRemove;
28: import cz.cvut.kbss.ontodriver.owlapi.change.SubjectDataPropertyRemove;
29: import cz.cvut.kbss.ontodriver.owlapi.change.SubjectObjectPropertyRemove;
30: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
31: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
32: import org.semanticweb.owlapi.model.IRI;
33: import org.semanticweb.owlapi.model.OWLAnnotationProperty;
34: import org.semanticweb.owlapi.model.OWLAnnotationValue;
35: import org.semanticweb.owlapi.model.OWLClass;
36: import org.semanticweb.owlapi.model.OWLDataFactory;
37: import org.semanticweb.owlapi.model.OWLDataProperty;
38: import org.semanticweb.owlapi.model.OWLIndividual;
39: import org.semanticweb.owlapi.model.OWLLiteral;
40: import org.semanticweb.owlapi.model.OWLNamedIndividual;
41: import org.semanticweb.owlapi.model.OWLObjectProperty;
42: import org.semanticweb.owlapi.model.OWLOntology;
43:
44: import java.util.ArrayList;
45: import java.util.Collection;
46: import java.util.List;
47: import java.util.Map;
48: import java.util.Set;
49: import java.util.stream.Collectors;
50:
51: class EpistemicAxiomRemover {
52:
53: private final OwlapiAdapter owlapiAdapter;
54: private final OWLOntology ontology;
55: private final OWLDataFactory dataFactory;
56: private final OntologySnapshot snapshot;
57:
58: EpistemicAxiomRemover(OwlapiAdapter adapter, OntologySnapshot snapshot) {
59: this.owlapiAdapter = adapter;
60: this.snapshot = snapshot;
61: this.ontology = snapshot.getOntology();
62: this.dataFactory = snapshot.getDataFactory();
63: }
64:
65: void remove(AbstractAxiomDescriptor descriptor) {
66: final List<TransactionalChange> changes = new ArrayList<>();
67: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(descriptor.getSubject(), dataFactory);
68: for (Assertion a : descriptor.getAssertions()) {
69: switch (a.getType()) {
70: case CLASS:
71: changes.add(new SubjectClassAssertionRemove(individual));
72: break;
73: case DATA_PROPERTY:
74: changes.add(new SubjectDataPropertyRemove(individual, dataFactory.getOWLDataProperty(IRI.create(a.getIdentifier()))));
75: break;
76: case OBJECT_PROPERTY:
77: changes.add(new SubjectObjectPropertyRemove(individual, dataFactory.getOWLObjectProperty(IRI.create(a.getIdentifier()))));
78: break;
79: case ANNOTATION_PROPERTY:
80: changes.add(new SubjectAnnotationPropertyRemove(individual, dataFactory.getOWLAnnotationProperty(IRI.create(a.getIdentifier()))));
81: break;
82: default:
83: break;
84: }
85: }
86: if (!changes.isEmpty()) {
87: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
88: }
89: }
90:
91: void removeAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> toRemove) {
92: final List<TransactionalChange> changes = new ArrayList<>();
93: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
94: for (Map.Entry<Assertion, Set<Value<?>>> e : toRemove.entrySet()) {
95: final IRI assertionIri = IRI.create(e.getKey().getIdentifier());
96: if (ontology.containsDataPropertyInSignature(assertionIri)) {
97: changes.addAll(removeDataPropertyAssertions(individual, e.getKey(), e.getValue()));
98: } else if (ontology.containsObjectPropertyInSignature(assertionIri)) {
99: changes.addAll(removeObjectPropertyAssertions(individual, e.getKey(), e.getValue()));
100: } else if (ontology.containsAnnotationPropertyInSignature(assertionIri)) {
101: changes.addAll(removeAnnotationAssertions(individual, e.getKey(), e.getValue()));
102: } else if (e.getKey().isClassAssertion()) {
103: changes.addAll(removeClassAssertionAxioms(individual, e.getValue()));
104: }
105: // It can happen that the assertionIri is no longer in the ontology, because of the way properties changes
106: // are processed in JOPA - they are compared to the original object, so if multiple property values are removed
107: // in a transaction, they are effectively removed multiple times. Therefore, it can happen that another
108: // property no longer exists in the ontology, because it was removed in the previous modifications during the same
109: // transaction
110: }
111: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
112: }
113:
114: private Collection<TransactionalChange> removeClassAssertionAxioms(OWLNamedIndividual individual, Set<Value<?>> values) {
115: return values.stream().map(value -> {
116: final OWLClass owlClass = dataFactory.getOWLClass(IRI.create(value.stringValue()));
117: return new MutableRemoveAxiom(ontology, dataFactory.getOWLClassAssertionAxiom(owlClass, individual));
118: }).collect(Collectors.toList());
119: }
120:
121: private Collection<TransactionalChange> removeDataPropertyAssertions(OWLNamedIndividual individual,
122: Assertion assertion, Set<Value<?>> values) {
123: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(assertion.getIdentifier()));
124: return values.stream().map(value -> {
125: final OWLLiteral literal = OwlapiUtils
126: .createOWLLiteralFromValue(value.getValue(),
127: OwlapiUtils.getAssertionLanguage(assertion));
128: return new MutableRemoveAxiom(ontology,
129: dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, individual, literal));
130: }).collect(Collectors.toList());
131: }
132:
133: private Collection<TransactionalChange> removeObjectPropertyAssertions(OWLNamedIndividual individual,
134: Assertion assertion, Set<Value<?>> values) {
135: final OWLObjectProperty objProperty = dataFactory.getOWLObjectProperty(IRI.create(assertion.getIdentifier()));
136: return values.stream().map(value -> {
137: final OWLIndividual object = OwlapiUtils
138: .getIndividual(NamedResource.create(value.stringValue()), dataFactory);
139: return new MutableRemoveAxiom(ontology,
140: dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty, individual, object));
141: }).collect(Collectors.toList());
142: }
143:
144: private Collection<TransactionalChange> removeAnnotationAssertions(OWLNamedIndividual individual,
145: Assertion assertion, Set<Value<?>> values) {
146: final OWLAnnotationProperty annProperty = dataFactory
147: .getOWLAnnotationProperty(IRI.create(assertion.getIdentifier()));
148: return values.stream().map(value -> {
149: OWLAnnotationValue av;
150: try {
151: av = IRI.create(value.stringValue());
152: } catch (IllegalArgumentException e) {
153: av = OwlapiUtils.createOWLLiteralFromValue(value.getValue(),
154: OwlapiUtils.getAssertionLanguage(assertion));
155: }
156: return new MutableRemoveAxiom(ontology,
157: dataFactory.getOWLAnnotationAssertionAxiom(annProperty, individual.getIRI(), av));
158: }).collect(Collectors.toList());
159: }
160: }